<!-- 登录页面 -->
<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form
          class="login_form"
          :rules="rules"
          ref="ruleFormRef"
          :model="loginForm"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
              :loading="btnlogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import type { FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
// 引入用户相关的仓库
import useuserStore from '@/store/modules/user'
import { getTime } from '@/utils/time'
getTime()
let userstore = useuserStore()
let router = useRouter()
let route = useRoute()
const ruleFormRef = ref()
let btnlogin = ref(false)
//收集账号与密码的数据
let loginForm = reactive({ username: 'admin', password: 'atguigu123' })
// 登录
const login = async () => {
  //保证全部表单相校验通过再发请求
  await ruleFormRef.value.validate()
  // 开启按钮login
  btnlogin.value = true
  try {
    await userstore.userLogin(loginForm)
    console.log(route)

    router.push({ path: (route.query.redirect as any) || '/' })
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: 'HI' + getTime() + '好!',
    })
    // 关闭按钮login
    btnlogin.value = false
  } catch (error) {
    // console.log(error)
    // 关闭按钮login
    btnlogin.value = false
  }
}
//自定义校验规则函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  //rule:即为校验规则对象
  //value:即为表单元素文本内容
  //函数:如果符合条件callBack放行通过即为
  //如果不符合条件callBack方法,注入错误提示信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}
// 表单校验
const rules = reactive<FormRules>({
  //规则对象属性:
  //required,代表这个字段务必要校验的
  //min:文本长度至少多少位
  //max:文本长度最多多少位
  //message:错误的提示信息
  //trigger:触发校验表单的时机 change->文本发生变化触发校验,blur:失去焦点的时候触发校验规则
  username: [{ trigger: 'change', validator: validatorUserName }],
  password: [{ trigger: 'change', validator: validatorPassword }],
})
</script>
<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: 100% 100%;

  .login_form {
    width: 60%;
    position: relative;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    padding: 20px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
  }
}
</style>
